<template>
  <div class="app-container">
    <div class="flr mb8">
      <div class="flbox">
        <el-button type="primary" plain size="mini" v-hasPermi="[]" @click="handleAdd">新增</el-button>
      </div>
      <right-toolbar @queryTable="getList"></right-toolbar>
    </div>
    <TableCom :columns="tableColumns" :options="tableOptions" @getList="getList"></TableCom>
  </div>
</template>

<script>
  import request from '@/utils/request'
  import { RSA_SIGN,RSA_NAME } from "@/utils/rsa256"

  export default {
    name: "Skey",
    data() {
      this.tableColumns = [
        { "prop": "serviceCode", "label": "系统编码", "width": 180,fixed:true },
        { "prop": "serviceName", "label": "系统名称", "width": 180 },
        { "prop": "servicePubkey", "label": "系统公钥", "width": 160,template:"ColumnSkeyMemo" },
        { "prop": "sysRsaPubkey", "label": "对应平台公钥", "width": 160,template:"ColumnSkeyMemo" },
        { "prop": "aesKey", "label": "AES_KEY", "minWidth": 240 },
        { "prop": "isEnable", "label": "启用", "width": 100,template:"ColumnSkeySwitch",submit:this.handleEnable },
        {
          label: "操作", prop: "_", width: "100", align: "center", fixed: "right", className: "", options: [
            { text: "修改", props: { type: 'primary' }, onClick: this.handleUpdate },
            { text: "删除", props: { type: 'danger' }, onClick: this.handleDelete },
          ]
        }
      ]
      //
      return {
        tableOptions:{
          pageNum:1,
          pageSize:10,
          data:[]
        }
      }
    },
    mounted() {
      this.getList();
    },
    methods: {
      getList(){
        let param = Object.assign(_.pick(this.tableOptions,['pageNum','pageSize']));
        param.pageNo = param.pageNum;
        request({
          url:"/rsakey/list",
          method:"post",
          data:param
        }).then((res)=>{
          let data = res.data;
        this.tableOptions.data = data.list;
        this.tableOptions.total = data.total;
      })
      },
      changeIsSet(d){
        this.dialogEdit().setFormOptions({
          aesKey:{hide:d.isSet == 1},
        })
      },
      handleAdd(){
        this.dialogEdit({
          row:{bizState:0},
          width:"40%",
          size:24,
          labelWidth:"8em",
          columns:[
            { text: "系统编码", name: "serviceCode", type: "input" },
            { text: "系统名称", name: "serviceName", type: "input" },
            { text: "系统公钥", name: "servicePubkey", type: "textarea" },
            { text:"AES_KEY",name:"isSet",type:"radio",value:"1",options:[{label:"系统生成",value:"1"},{label:"自定义",value:"2"}],onChange:this.changeIsSet },
            { text: "", name: "aesKey", type: "input",hide:true,placeholder:"请输入AES_KEY" },
            { text: "启用", name: "isEnableText", type: "switch" },
          ],
          rules:{
            serviceCode: ["required"],
            serviceName: ["required"],
            servicePubkey: ["required"],
            sysRsaPubkey: ["required"],
            aesKey:["required"]
          },
          formatter:this.submit
        },this.getList)
      },
      handleUpdate(row){
        row.isEnableText = row.isEnable == 0;
        this.dialogEdit({
          row:row,
          width:"40%",
          size:24,
          labelWidth:"8em",
          columns:[
            { text: "系统编码", name: "serviceCode", type: "input" },
            { text: "系统名称", name: "serviceName", type: "input" },
            { text: "系统公钥", name: "servicePubkey", type: "textarea" },
            { text: "系统私钥", name:"servicePrikey", type:"textarea" },
            { text:"AES_KEY",name:"isSet",type:"radio",value:"1",options:[{label:"系统生成",value:"1"},{label:"自定义",value:"2"}],onChange:this.changeIsSet },
            { text: "", name: "aesKey", type: "input",hide:true,placeholder:"请输入AES_KEY" },
            { text: "启用", name: "isEnableText", type: "switch" },
          ],
          rules:{
            serviceCode: ["required"],
            serviceName: ["required"],
            servicePubkey: ["required"],
            servicePrikey: ["required"],
            aesKey:["required"]
          },
          formatter:this.submit
        },this.getList)
      },
      handleDelete(row){
        this.dialogEdit({
          title:"删除",
          row:{id:row.id,serviceCode:row.serviceCode},
          width:"40%",
          size:24,
          labelWidth:"6em",
          columns:[
            { text: "系统编码", name: "serviceCode", type: "input",props:{disabled:true} },
            { text: "系统私钥", name: "servicePrikey", type: "textarea" },
          ],
          rules:{
            servicePrikey: ["required"]
          },
          formatter:(data)=>{
          request({
                    headers:{
                      Accept: 'application/json',
                      // service_code:data.serviceCode,
                      // sign:RSA_SIGN(data.servicePrikey,JSON.stringify(data.id))
                    },
                    url:"/rsakey/deleteById",
                    method:"post",
                    data:data
                  }).then((res)=>{
          this.$message.success("删除成功");
        this.dialogEdit().onClose();
        this.getList();
      })
      }
      },this.getList)
      },
      handleEnable(data){
        let param = {id:data.id};
        param.isEnable = data.isEnableText ? 0 : 1;
        if(data.id){
          request({
            headers:{
              Accept: 'application/json',
            },
            url:"/rsakey/updateEnable",
            method:"post",
            data:param
          }).then((res)=>{
            this.$message.success("修改成功");
          this.dialogEdit().onClose();
          this.getList();
        })
        }
      },
      submit(data){
        let param = _.pick(data,["serviceCode",'serviceName','servicePubkey','servicePrikey','id'])
        param.isEnable = data.isEnableText ? 0 : 1;
        param.aesKey = data.isSet == 1 ? null : data.aesKey;
        if(data.id){
          request({
            headers:{
              Accept: 'application/json',
              // service_code:data.serviceCode, //data.serviceCode,
              // sign:RSA_SIGN(data.servicePrikey,JSON.stringify(param))
            },
            url:"/rsakey/updateById",
            method:"post",
            data:param
          }).then((res)=>{
            this.$message.success("修改成功");
          this.dialogEdit().onClose();
          this.getList();
        })
        }else{
          request({
            headers:{
              Accept: 'application/json',
              // service_code:RSA_NAME,
              // sign:RSA_SIGN('',JSON.stringify(param))
            },
            url:"/rsakey/add",
            method:"post",
            data:param
          }).then((res)=>{
            this.$message.success("保存成功");
          this.dialogEdit().onClose();
          this.getList();
        })
        }
      }
    }
  };
</script>
